package com.distribute.framework.model;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.distribute.framework.core.lang.caption;

public class DefineException extends RuntimeException {

	String code;
	
	public DefineException(String code, String message) {
		super(message);
		this.code = code;
	}

	public DefineException(String string) {
		super(string);
	}

	public DefineException() {
	}

	/**
	 * @param message
	 * @param cause
	 */
	public DefineException(String message, Throwable cause) {
		super(message, cause);
	}

	/**
	 * @param cause
	 */
	public DefineException(Throwable cause) {
		super(cause);
	}
	
	public static String parseException(Exception ex,Class modelClass) {
		if(ex.getMessage()==null)return null;
		Matcher columnull = Pattern.compile("Column '([^']+)' cannot be null").matcher(ex.getMessage());
		if(columnull.find()){
			Field field=null;
			try {
				field = modelClass.getDeclaredField(Utils.forMat(columnull.group(1)));
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
			caption caption = field.getAnnotation(caption.class);
			return ("'"+caption.value()+"'不能为空或未输入");
		}
		return null;
	}
	
	public String getCode() {
		return code;
	}


	public void setCode(String code) {
		this.code = code;
	}
	
}